﻿using Client.Constant;
using Client.Enum;
using Client.Framework.Event;
using Client.Framework.Message;
using Client.Framework.Socket;
using Client.Message.Request;
using Client.Message.Response;
using Client.Model;
using Client.Service;
using Client.View;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace Client.ViewModel
{
    public class RoleEditViewModel : BindableBase
    {

        public RoleEdit Page { get; private set; }

        public RoleEditViewModel(RoleEdit page)
        {
            Page = page;

            SaveCommand = new DelegateCommand(SaveInfo);

            MsgEventSubscribe();
        }

        private void MsgEventSubscribe()
        {
            MessageEventPublisher.SubscribeEvent(MessageId.OPT_RESP, ReceiveOptMsg);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init(ViewRole role, RoleManagePageViewModel roleManageDataContext)
        {
            RoleManageDataContext = roleManageDataContext;
            Title = role == null ? "新增角色" : "编辑角色";
            if (role == null)
            {
                ViewRole = new ViewRole();
                return;
            }
            ViewRole = role;
        }

        /// <summary>
        /// 标题
        /// </summary>
        private string title;
        public string Title
        {
            get { return title; }
            set
            {
                title = value;
                RaisePropertyChanged("Title");
            }
        }

        /// <summary>
        /// 角色信息
        /// </summary>
        private ViewRole viewRole;
        public ViewRole ViewRole
        {
            get { return viewRole; }
            set
            {
                viewRole = value;
                RaisePropertyChanged("ViewRole");
            }
        }

        /// <summary>
        /// 父级页面的DataContext
        /// </summary>
        private RoleManagePageViewModel RoleManageDataContext { get; set; }

        /// <summary>
        /// 保存角色信息
        /// </summary>
        public DelegateCommand SaveCommand { get; private set; }
        public void SaveInfo()
        {
            AddEditRoleReq msg = new AddEditRoleReq(MessageId.ADD_EDIT_ROLE)
            {
                Role = viewRole
            };
            ApplicationClient.Instance().Send(msg);
        }

        /// <summary>
        /// 操作回复消息接收
        /// </summary>
        /// <param name="message">回复消息</param>
        private void ReceiveOptMsg(ResponseBaseMessage message)
        {
            if (message is OperationResp msg)
            {
                if (msg.OptType != (int)OptType.ADD_EDIT_ROLE) return;
                System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    ConfirmTip tip = new ConfirmTip()
                    {
                        Message = { Text = msg.Content }
                    };
                    DialogHost.Show(tip, "RoleEditPage", CloseTip);
                }));                
            }
        }




        public void CloseTip(object sender, DialogClosingEventArgs eventArgs)
        {
            Page.Hide();
            if (RoleManageDataContext != null)
            {
                RoleService.Instance().RefreshRoles();
                RoleManageDataContext.GetRoles();
            }
        }




    }

    


}
